package com.zsj.LinkedList;

import java.util.Iterator;
import java.util.function.Consumer;

/**
 * @version 2.0
 * @Author zsj
 * @Date 2023/10/22 14:28
 * @Descripiton 单向链表
 * 加入哨兵
 */
public class SinglyLinkedList implements Iterable<Integer> {

    private Node head = new Node(666, null);//头指针

    /**
     * 节点内部实现类
     */
    private static class Node {
        int value;//值
        Node next;//下一个节点

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }


    /**
     * 将元素插入链表头部
     */
    public void addFirst(int value) {
        //链表为空或不为空
        insert(0, value);
    }

    /**
     * 正常插入到尾部
     */
    public void addLast(int value) {
        Node lastNode = findLastNode(head);
        lastNode.next = new Node(value, null);
    }

    /**
     * 寻找最后一个节点
     */
    private Node findLastNode(Node head) {
        while (head.next != null) {
            head = head.next;
        }
        return head;
    }

    /**
     * 获取索引位置的值
     */
    public int get(int index) {
        Node node = findNode(index);
        if (node == null) throw new IllegalArgumentException(String.format("index %d not legal", index));
        return node.value;
    }

    /**
     * 根据索引获取节点
     */
    private Node findNode(int index) {
        int begin = -1;
        for (Node p = head; p != null; p = p.next, begin++) {
            if (begin == index) return p;
        }
        return null;
    }

    /**
     * 根据索引插入
     */
    public void insert(int index, int value) {
        Node pre = findNode(index - 1);
        if (pre == null) {
            throw new IllegalArgumentException(String.format("index %d is not legal", index));
        }
        pre.next = new Node(value, pre.next);

    }

    /**
     * 删除第一个节点
     * 不是删除头节点
     */
    public void removeFirst() {
        remove(0);
    }

    /**
     * 删除指定索引位置的元素
     *
     * @param index 索引
     */
    public void remove(int index) {
        Node node = findNode(index - 1);
        if (node == null) {
            throw new IllegalArgumentException("prev node is not exist");
        }
        Node removed = node.next;
        if (removed == null) {
            throw new IllegalArgumentException("need remove's node is not exist");
        }
        node.next = removed.next;
    }

    /**
     * while循环遍历
     */
    public void loop(Consumer<Integer> consumer) {
        Node p = head.next;
        while (p != null) {
            consumer.accept(p.value);
            p = p.next;
        }
    }

    /**
     * for循环遍历
     */
    public void loop2(Consumer<Integer> consumer) {
        for (Node p = head.next; p != null; p = p.next) {
            consumer.accept(p.value);
        }
    }

    /**
     * 递归遍历
     */
    public void loop3() {
        recursion(head.next);
    }

    /**
     * 递归方法
     */
    private void recursion(Node head) {
        if (head != null) {
            System.out.println(head.value);
            recursion(head.next);
        }
    }

    /**
     * 迭代器遍历
     * 增强for
     */
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p = head.next;

            @Override
            public boolean hasNext() {
                return p != null;
            }

            @Override
            public Integer next() {
                Integer value = p.value;
                p = p.next;
                return value;
            }
        };
    }


}


